package com.netease.framework.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

public class CustomAlertDialog implements DialogInterface.OnClickListener {

	public static interface DialogListener {
		public void onClick(DialogInterface DialogListn, 
				int which, int key, View view);
	}
	
	DialogListener mDialogListner;
	int mKey;
	View mView;
	
	public CustomAlertDialog(int key, DialogListener dialogListener) {
		mKey = key;
		mDialogListner = dialogListener;
	}
	
	private static AlertDialog.Builder builder(Context context, 
			int title, int message) {
		AlertDialog.Builder builder = null;
//        builder = new AlertDialog.Builder(context);
		builder = DialogUtil.newAlertDiag(context);
        if (title >=0 ) {
        	builder.setTitle(title);
        }
        
        if (message >=0 ) {
        	builder.setMessage(message);
        }
        
        return builder;
	}
	
	private static AlertDialog.Builder builder(Context context, 
			int title, int message, int icon) {
		AlertDialog.Builder builder = builder(context, title, message);
		if (icon >= 0) {
		    builder.setIcon(icon);
		}
        return builder;
	}
	
	private static AlertDialog.Builder builder(Context context, 
			int title, int message, int icon, int positive) {
		AlertDialog.Builder builder = builder(context, title, message, icon);
		builder.setPositiveButton(positive, null);
        return builder;
	}
	
	private static AlertDialog.Builder builder(Context context, 
			int title, int message, int icon, int positive, int nagetive) {
		AlertDialog.Builder builder = builder(context, 
				title, message, icon, positive);
		builder.setNegativeButton(nagetive, null);
        return builder;
	}
	
	private static AlertDialog.Builder builder(Context context, 
			int title, int message, int icon, int key,
			int positive, DialogListener listener) {
		AlertDialog.Builder builder = builder(context, 
				title, message, icon);
		CustomAlertDialog dialog = new CustomAlertDialog(key, listener);
		builder.setPositiveButton(positive, dialog);
        return builder;
	}
	
	private static AlertDialog.Builder builder(Context context, 
			int title, int message, int icon, int key, int positive, 
			int nagetive, DialogListener listener) {
		AlertDialog.Builder builder = builder(context, 
				title, message, icon);
		CustomAlertDialog dialog = new CustomAlertDialog(key, listener);
		builder.setPositiveButton(positive, dialog);
		builder.setNegativeButton(nagetive, dialog);
        return builder;
	}
	
	private static AlertDialog.Builder builder(Context context, 
			int title, int message, int icon, int key,
			View view, int positive, DialogListener listener) {
		AlertDialog.Builder builder = builder(context, title, message,
		        icon, key, view, positive, -1, listener);
        return builder;
	}
	
	private static AlertDialog.Builder builder(Context context, 
			int title, int message, int icon, int key, View view,
			int positive, int nagetive, DialogListener listener) {
		AlertDialog.Builder builder = builder(context, 
				title, message, icon);
		CustomAlertDialog dialog = new CustomAlertDialog(key, listener);
		dialog.mView = view;
		builder.setView(view);
		if (positive >= 0 ) {
		    builder.setPositiveButton(positive, dialog);
		}
		
		if (nagetive >= 0) {
		    builder.setNegativeButton(nagetive, dialog);
		}
		
        return builder;
	}
	
	public static void showAlertDialog(Context context,
			int title, int message) {
		builder(context, title, message).show();
	}
	
	public static void showAlertDialog(Context context,
			int title, int message, int icon) {
		builder(context, title, message, icon).show();
	}
	
	public static void showAlertDialog(Context context,
			int title, int message, int icon, int positive) {
		builder(context, title, message, icon, positive).show();
	}
	
	public static void showAlertDialog(Context context,
			int title, int message, int icon, int positive, int nagetive) {
		builder(context, title, message, icon, positive, nagetive).show();
	}
	
	public static void showAlertDialog(Context context, int key,
			int title, int message, int icon, int positive, 
			DialogListener listener) {
		builder(context, title, message, icon, key, positive, listener).show();
	}
	
	public static void showAlertDialog(Context context, int key,
			int title, int message, int icon, int positive, int nagetive, 
			DialogListener listener) {
		builder(context, title, message, icon, key, 
				positive, nagetive, listener).show();
	}
	
	public static AlertDialog showAlertDialog(Context context, int key,
			int title, int message, int icon, View view, int positive, 
			DialogListener listener) {
		return builder(context, title, message, icon, key, 
				view, positive, listener).show();
	}
	
	public static AlertDialog showAlertDialog(Context context, int key,
			int title, int message, int icon, View view, 
			int positive, int nagetive, DialogListener listener) {
		return builder(context, title, message, icon, key, 
				view, positive, nagetive, listener).show();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		if (mDialogListner != null) {
			mDialogListner.onClick(dialog, which, mKey, mView);
		}
		
		mDialogListner = null;
		mView = null;
		dialog.dismiss();
	}
	
}
